package com.eningqu.aipen.ui.medical;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import com.bumptech.glide.Glide;
import com.eningqu.aipen.demo.R;
import com.eningqu.aipen.demo.databinding.ActivityPreviewPicBinding;
import com.eningqu.aipen.demo.databinding.ItemPicPreBinding;
import com.eningqu.aipen.ui.base.BaseActivity;

import java.util.List;

/**
 * Package:com.eningqu.aipen.ui.medical
 * Author:starr
 * Time:11/24/2023 3:26 PM
 * Description: 图片预览工具类
 */
public class PreviewPicActivity extends BaseActivity<ActivityPreviewPicBinding> {

    private static List<String> list;
    private static int position;

    private ViewPager2 viewPager2;
    public static void startAction(Context context, Bundle bundle){
        Intent intent=new Intent(context,PreviewPicActivity.class);
        list=bundle.getStringArrayList("img");
        position=bundle.getInt("pos");
        context.startActivity(intent);
    }
    @Override
    protected int getLayoutId() {
        return R.layout.activity_preview_pic;
    }

    @Override
    protected void initView() {
        viewPager2=mBinding.viewPager22;

        mBinding.close.setOnClickListener(v ->{
            finish();
        });

    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void initData() {
        MyAdapter adapter=new MyAdapter();
        viewPager2.setAdapter(adapter);
        viewPager2.setCurrentItem(position);
        mBinding.position.setText((position+1)+"/"+(list.size()));
        viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                mBinding.position.setText((position+1)+"/"+(list.size()));

            }
        });
    }

    @Override
    protected void async() {

    }

    public class PreviewAction{
        public void finish(View view){
            PreviewPicActivity.this.finish();
        }

    }

    private static class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{

        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            ItemPicPreBinding binding= DataBindingUtil.inflate(LayoutInflater.from(viewGroup.getContext()),R.layout.item_pic_pre,viewGroup,false);
            MyViewHolder holder=new MyViewHolder(binding.getRoot());
            return holder;
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, int i) {
            ItemPicPreBinding binding=DataBindingUtil.getBinding(holder.itemView);
            assert binding != null;
            Glide.with(binding.getRoot())
                    .load(list.get(i))
                    .placeholder(R.drawable.pic_empty)
                    .into(binding.photoview);
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        protected static class MyViewHolder extends RecyclerView.ViewHolder{

            public MyViewHolder(@NonNull View itemView) {
                super(itemView);
            }
        }
    }


}
